import { ConfigManager } from "../../../com/gengine/resource/ConfigManager";
import { SmeltPlanData } from "../view/smelt/data/SmeltPlanData";
import { StringHelper } from "../../../com/gengine/utils/StringHelper";
import { TJewelTransform } from "../../../Message/Db/Tables/TJewelTransform";
import { ClockManager } from "../manager/ClockManager";
import { DateParser } from "../../common/tools/DateParser";
import { Global } from "../../../com/gengine/global/Global";
import { Cache } from "../cache/Cache";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { ParamsConst } from "../../common/global/ParamsConst";
import { ProxyType } from "../../common/global/ProxyType";
import { Language } from "../../../extend/language/Language";
import { SmeltTypeData } from "../view/smelt/data/SmeltTypeData";
import { CareerUtil } from "../view/common/util/CareerUtil";
import { ItemData } from "./info/ItemData";
import { TSmeltType } from "../../../Message/Db/Tables/TSmeltType";
type int = number;
//class SmeltConfig
    
    export  class SmeltConfig
    {
        constructor()
        {
            
            this._categoryMap = new Map<any, any> /* flash.utils.Dictionary */();
            this._typeMap = new Map<any, any> /* flash.utils.Dictionary */();
            this._planMap = new Map<any, any> /* flash.utils.Dictionary */();
            this._jewelTransMap = new Map<any, any> /* flash.utils.Dictionary */();
            this._smeltMaterialMap = new Map<any, any> /* flash.utils.Dictionary */();
            if (SmeltConfig._instance != null) 
            {
                throw new Error(" SmeltConfig 单例 ");
            }
            this.init();
            return;
        }

        public init(): void
        {
            var loc1=null;
            loc1 = ConfigManager.instance.getJSONByFileName("t_smelt_category.json");
            this.writeCategory(loc1);
            loc1 = ConfigManager.instance.getJSONByFileName("t_smelt_type.json");
            this.writeType(loc1);
            loc1 = ConfigManager.instance.getJSONByFileName("t_smelt_plan.json");
            this.writePlan(loc1);
            loc1 = ConfigManager.instance.getJSONByFileName("t_jewel_transform.json");
            this.writeJewelTransform(loc1);
            return;
        }

        /* internal  */writeCategory(arg1: Object): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=arg1;
            for(loc1 of loc3) 
            {
                this._categoryMap[loc1["smeltCategory"]] = loc1;
            }
            return;
        }

        /* internal  */writeType(arg1: Object): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=arg1;
            for(loc1 of loc3) 
            {
                this._typeMap[loc1["smeltType"]] = loc1;
            }
            return;
        }

        /* internal  */writePlan(arg1: Object): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=arg1;
            for(loc2 of loc4) 
            {
                loc1 = new SmeltPlanData();
                loc1.smeltPlanCode = loc2.smeltPlanCode;
                loc1.showLevel = loc2.showLevel;
                loc1.minLevel = loc2.minLevel;
                loc1.maxLevel = loc2.maxLevel;
                loc1.smeltPlanCoin = loc2.smeltPlanCoin;
                loc1.smeltMainProduce = StringHelper.getString(loc2.smeltMainProduce);
                loc1.smeltPlanDesc = StringHelper.getString(loc2.smeltPlanDesc);
                loc1.smeltExtendProduce = StringHelper.getString(loc2.smeltExtendProduce);
                loc1.smeltMaterialList = StringHelper.getString(loc2.smeltMaterialList);
                loc1.defaultSmeltNum = loc2.defaultSmeltNum;
                loc1.career = loc2.career;
                this._planMap[loc2["smeltPlanCode"]] = loc1;
                this.addToMaterialMap(loc1);
            }
            return;
        }

        /* internal  */addToMaterialMap(arg1: SmeltPlanData): void
        {
            var loc2=null;
            var loc1=arg1.materials;
            var loc3=0;
            var loc4=loc1;
            for(loc2 of loc4) 
            {
                if (!this._smeltMaterialMap[loc2.itemBindCode]) 
                {
                    this._smeltMaterialMap[loc2.itemBindCode] = arg1;
                }
                if (this._smeltMaterialMap[loc2.itemUnBindCode]) 
                {
                    continue;
                }
                this._smeltMaterialMap[loc2.itemUnBindCode] = arg1;
            }
            return;
        }

        /* internal  */writeJewelTransform(arg1: Object): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=arg1;
            for(loc2 of loc4) 
            {
                loc1 = new TJewelTransform();
                loc1.code = loc2.code;
                loc1.cost = loc2.cost;
                loc1.tranList = StringHelper.getString(loc2.tranList);
                loc1.id = loc2.id;
                loc1.type = loc2.type;
                loc1.unit = loc2.unit;
                this._jewelTransMap[loc1.code + "_" + loc1.type] = loc1;
            }
            return;
        }

        public getCategoryArray(): Array<any>
        {
            var loc2=null;
            var loc3=null;
            var loc5=null;
            var loc1=ClockManager.instance.nowDate;
            var loc4=[];
            var loc6=0;
            var loc7=this._categoryMap;
            for(loc5 of loc7) 
            {
                loc2 = DateParser.strToDateNormal(loc5.startDt);
                loc3 = DateParser.strToDateNormal(loc5.endDt);
                if (!(Global.isDebugModle || loc2.time <= loc1.time && loc3.time > loc1.time || loc5.smeltCategory == 6 && Cache.instance.active.isActiveInEffect(EActiveType._EActiveTypeSemlt))) 
                {
                    continue;
                }
                if (ParamsConst.instance.proxyType != ProxyType.TW) 
                {
                    loc5.name = loc5.smeltCategoryName;
                    loc5.label = loc5.smeltCategoryName;
                }
                else if (loc5.activeSmelt != 1) 
                {
                    loc5.name = loc5.smeltCategoryName;
                    loc5.label = loc5.smeltCategoryName;
                }
                else 
                {
                    loc5.name = loc5.smeltCategoryName;
                    loc5.label = Language.getString(380010);
                }
                loc4.push(loc5);
            }
            loc4.sortOn("smeltCategoryIndex", Array.NUMERIC);
            return loc4;
        }

        public getTypeListByCategory(arg1: int): Array<any>
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=null;
            var loc7=0;
            var loc1=[];
            var loc8=0;
            var loc9=this._typeMap;
            for(loc6 of loc9) 
            {
                if (loc6.smeltCategory != arg1) 
                {
                    continue;
                }
                loc2 = new SmeltTypeData();
                loc2.smeltCategory = loc6.smeltCategory;
                loc2.smeltType = loc6.smeltType;
                loc2.smeltTypeName = StringHelper.getString(loc6.smeltTypeName);
                loc2.smeltPlanList = new Array<any>();
                loc4 = (StringHelper.getString(loc6.smeltPlanList) as string).split(",");
                loc7 = 0;
                while (loc7 < loc4.length) 
                {
                    if (loc4[loc7] != "") 
                    {
                        loc5 = parseInt(loc4[loc7]);
                        if ((loc3 = this.getPlanById(loc5)).career == 0 || CareerUtil.isCareerSuit(loc3.career, Cache.instance.role.careerBasic)) 
                        {
                            loc2.smeltPlanList.push(loc3);
                        }
                    }
                    ++loc7;
                }
                loc1.push(loc2);
            }
            loc1.sortOn("smeltType", Array.NUMERIC);
            return loc1;
        }

        public getPlanById(arg1: int): SmeltPlanData
        {
            return this._planMap[arg1];
        }

        public getJewelTransCostByCode(arg1: int, arg2: int=0): int
        {
            var loc1=this._jewelTransMap[arg1 + "_" + arg2];
            if (loc1) 
            {
                return loc1.cost;
            }
            return 0;
        }

        public getTransListByCode(arg1: int, arg2: int=0): Array<any>
        {
            var loc3=null;
            var loc4=0;
            var loc1=[];
            var loc2;
            if (loc2 = this._jewelTransMap[arg1 + "_" + arg2]) 
            {
                loc3 = loc2.tranList.split(",");
                loc4 = 0;
                while (loc4 < loc3.length) 
                {
                    loc1.push(new ItemData(parseInt(loc3[loc4])));
                    ++loc4;
                }
            }
            return loc1;
        }

        public checkJewelCanExchange(arg1: int, arg2: int=0): Boolean
        {
            if (this._jewelTransMap[arg1 + "_" + arg2]) 
            {
                return true;
            }
            return false;
        }

        public getNewJewelDataByType(arg1: int, arg2: int): ItemData
        {
            var loc1=null;
            var loc2=this.getTransListByCode(arg1);
            var loc3=0;
            while (loc3 < loc2.length) 
            {
                loc1 = loc2[loc3] as ItemData;
                if (loc1.type == arg2) 
                {
                    return loc1;
                }
                ++loc3;
            }
            return null;
        }

        public getSmeltPlanByMaterial(arg1: int): SmeltPlanData
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc1=this._smeltMaterialMap[arg1];
            if (loc1) 
            {
                loc2 = this.getSmeltTypeByPlan(loc1);
                if (loc2) 
                {
                    if (loc3 = this._categoryMap[loc2.smeltCategory]) 
                    {
                        loc4 = ClockManager.instance.nowDate;
                        loc5 = DateParser.strToDateNormal(loc3.startDt);
                        loc6 = DateParser.strToDateNormal(loc3.endDt);
                        if (loc5.time <= loc4.time && loc6.time > loc4.time) 
                        {
                            return loc1;
                        }
                    }
                    return null;
                }
                return null;
            }
            return null;
        }

        public getSmeltCategoryByType(arg1: int): int
        {
            var loc1=this._typeMap[arg1];
            if (loc1) 
            {
                return loc1["smeltCategory"];
            }
            return 0;
        }

        public isActiveSmeltPlan(arg1: SmeltPlanData): Boolean
        {
            var loc3=null;
            var loc1=this.getSmeltTypeByPlan(arg1);
            if (!loc1) 
            {
                return false;
            }
            var loc2=this.getSmeltCategoryByType(loc1.smeltType);
            var loc4=0;
            var loc5=this._categoryMap;
            for(loc3 of loc5) 
            {
                if (loc3["smeltCategory"] != loc2) 
                {
                    continue;
                }
                return loc3["activeSmelt"] > 0;
            }
            return false;
        }

        public getSmeltTypeByPlan(arg1: SmeltPlanData): TSmeltType
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=this._typeMap;
            for(loc1 of loc5) 
            {
                loc2 = loc1["smeltPlanList"];
                if (loc2.indexOf(String(arg1.smeltPlanCode)) == -1) 
                {
                    continue;
                }
                (loc3 = new TSmeltType()).smeltType = loc1.smeltType;
                loc3.smeltCategory = loc1.smeltCategory;
                loc3.smeltPlanList = loc1.smeltPlanList;
                loc3.smeltTypeName = loc1.smeltTypeName;
                return loc3;
            }
            return null;
        }

        public static get instance(): SmeltConfig
        {
            if (!SmeltConfig._instance) 
            {
                SmeltConfig._instance = new SmeltConfig();
            }
            return SmeltConfig._instance;
        }

        /* internal */ /* var */_categoryMap: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_typeMap: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_planMap: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_jewelTransMap: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_smeltMaterialMap: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ static /* var */_instance: SmeltConfig;
    }
